package com.example.demo.Controller;


import com.example.demo.core.JsonUtil;
import com.example.demo.core.NotFoundException;
import com.example.demo.core.Response;
import com.example.demo.model.Book;
import com.example.demo.service.BookBorrowAndFeeService;
import com.example.demo.service.BookService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    BookService bookServiceImpl;

    @Autowired
    BookBorrowAndFeeService bookBorrowAndFeeServiceImpl;


    @GetMapping("/info/select/all")
    @RequiresPermissions(logical = Logical.AND, value = {"book:info:select:all"})
    public Object bookSelectByCondition(Integer bookinfoId,String bookname, String authorname, String press,String introduction,String isbn,Double price,
                                        @RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "20") Integer limit) throws NotFoundException {
        if ("".equals(bookname))
            bookname=null;
        if ("".equals(authorname))
            authorname=null;
        if ("".equals(press))
            press=null;
        if ("".equals(introduction))
            introduction=null;
        if ("".equals(isbn))
            isbn=null;

        List<Book> books = bookServiceImpl.selectByCondition(bookinfoId, bookname, authorname, press, introduction, isbn, price, "0");

        if (books.size()==0)
            throw new NotFoundException("查询的书为空");
        books.stream().forEach(x->x.setIfBack(x.getIfBack()==null?1:0));
        return Response.okList(books,page,limit);//发list就行
    }

    @PostMapping("info/add")
    @RequiresRoles(logical = Logical.OR,value = {"default_admin", "super_admin"})
    @RequiresPermissions(logical = Logical.AND,value = {"book:info:add"})
    public Object addBook(@RequestBody String body, HttpServletRequest request) {

        String bookinfoIdString= JsonUtil.parseString(body, "bookinfoId");
        Integer bookinfoId=null;
        if (bookinfoIdString!=null)
        {
            bookinfoId=Integer.parseInt(bookinfoIdString);
            if (bookServiceImpl.selectByCondition(bookinfoId,null,null,null,null,null,null,null).size()!=0)
            {
                //说明重复bookinfoId
                return Response.fail(401,"该索书号条形码已经存在");
            }
        }
        String bookname = JsonUtil.parseString(body, "bookname");

        String authorname = JsonUtil.parseString(body, "authorname");

        String press = JsonUtil.parseString(body, "press");

        String introduction = JsonUtil.parseString(body, "introduction");

        String isbn = JsonUtil.parseString(body, "isbn");

        Double price = null;
        if (JsonUtil.parseString(body, "price") != null)
            price = Double.parseDouble(JsonUtil.parseString(body, "price"));

        if (bookinfoId==null && bookname ==null && authorname ==null && press ==null && introduction ==null && isbn ==null && price==null)
            return Response.fail(401,"参数不能为全空");

        return Response.ok(bookServiceImpl.bookInsert(new Book(bookinfoId, bookname, authorname, press, introduction, isbn, price)));
    }

    @PostMapping("info/update")
    @RequiresRoles(logical = Logical.OR,value = {"default_admin", "super_admin"})
    @RequiresPermissions(logical = Logical.AND,value = {"book:info:update"})
    public Object updateBook(@RequestBody String body, HttpServletRequest request) {

        if (JsonUtil.parseString(body, "bookinfoId") == null)
            return Response.badArgumentValue("id");
        Integer bookinfoId = Integer.parseInt(JsonUtil.parseString(body, "bookinfoId"));

        String bookname = JsonUtil.parseString(body, "bookname");

        String authorname = JsonUtil.parseString(body, "authorname");

        String press = JsonUtil.parseString(body, "press");

        String introduction = JsonUtil.parseString(body, "introduction");

        String isbn = JsonUtil.parseString(body, "isbn");

        Double price = null;
        if (JsonUtil.parseString(body, "price") != null)
            price = Double.parseDouble(JsonUtil.parseString(body, "price"));

        return Response.ok(bookServiceImpl.bookUpdate(new Book(bookinfoId, bookname, authorname, press, introduction, isbn, price)));
    }

    @PostMapping("info/delete")
    @RequiresRoles(logical = Logical.OR,value = {"default_admin", "super_admin"})
    @RequiresPermissions(logical = Logical.AND,value = {"book:info:delete"})
    public Object deleteBook(@RequestBody String body, HttpServletRequest request) {
        if (JsonUtil.parseString(body, "bookinfoId") == null)
            return Response.badArgumentValue("id");
        Integer bookinfoId = Integer.parseInt(JsonUtil.parseString(body, "bookinfoId"));
        if(bookBorrowAndFeeServiceImpl.isBorrow(bookinfoId,null))
            return Response.fail(401,"该书已被借，不可删除此书");
        return Response.ok(bookServiceImpl.bookDelete(new Book(bookinfoId, null, null, null, null, null, null)));
    }
}
